﻿namespace System.ServiceModel.DomainServices.Client
{
    using System;
    using System.Collections.Generic;

    public sealed class InvokeOperation<TValue> : InvokeOperation
    {
        private Action<InvokeOperation<TValue>> _cancelAction;
        private Action<InvokeOperation<TValue>> _completeAction;

        internal InvokeOperation(string operationName, IDictionary<string, object> parameters, Action<InvokeOperation<TValue>> completeAction, object userState, Action<InvokeOperation<TValue>> cancelAction) : base(operationName, parameters, null, userState, null)
        {
            this._cancelAction = cancelAction;
            this._completeAction = completeAction;
        }

        protected override void CancelCore()
        {
            this._cancelAction((InvokeOperation<TValue>) this);
        }

        protected override void InvokeCompleteAction()
        {
            if (this._completeAction != null)
            {
                this._completeAction((InvokeOperation<TValue>) this);
            }
        }

        protected override bool SupportsCancellation
        {
            get
            {
                return (this._cancelAction != null);
            }
        }

        public TValue Value
        {
            get
            {
                if (base.Result == null)
                {
                    return default(TValue);
                }
                return (TValue) base.Result.ReturnValue;
            }
        }
    }
}

